package ru.padre386.geo_utils.reports.console;

import ru.padre386.geo_utils.VectorUtils;
import ru.padre386.geo_utils.data_loaders.common.IFileIteratorCallback;
import ru.padre386.geo_utils.data_loaders.common.IGeoPoint;
import ru.padre386.geo_utils.data_loaders.common.IGeoTrack;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by IntelliJ IDEA.
 * User: indrdb
 * Date: 08.02.2011
 * Time: 11:22:26
 */
public class PerDateStatCalculatorFileIteratorCallbackImpl implements IFileIteratorCallback {
    private GregorianCalendar lastDateStart;
    private GregorianCalendar lastDateFinish;

    private double dayIncreaseAlt = 0;
    private double dayDecreaseAlt = 0;
    private double dayWay = 0;
    IGeoPoint lastPoint = null;
    private double totalWay = 0;

    private VectorUtils vectorUtils = new VectorUtils();
    private NumberFormat numberFormat = new DecimalFormat();

    @Override
    public void onStart() {
        lastDateStart = null;
        lastDateFinish = null;

        dayIncreaseAlt = 0;
        dayDecreaseAlt = 0;
        dayWay = 0;
        totalWay = 0;

        lastPoint = null;
    }

    @Override
    public void onTrack(IGeoTrack track) {
    }


    @Override
    public void onPoint(IGeoPoint point) {
        final GregorianCalendar pointGc = new GregorianCalendar();
        pointGc.setTime(point.getTime());
        if (lastDateFinish == null) {
            lastDateStart = pointGc;
            lastDateFinish = pointGc;
            lastPoint = point;

            System.out.println("Track start at: " + lastDateFinish.getTime());
        } else {
            if (lastDateFinish.get(Calendar.DAY_OF_YEAR) != pointGc.get(Calendar.DAY_OF_YEAR)) {
                System.out.println("Next day:");
                System.out.println("\tStart time: " + lastDateStart.getTime());
                System.out.println("\tFinish time: " + lastDateFinish.getTime());
                System.out.println("\tTotal increase alt: " + numberFormat.format(dayIncreaseAlt) + "m");
                System.out.println("\tTotal decrease alt: " + numberFormat.format(dayDecreaseAlt) + "m");

                System.out.println("\tTotal trip distance: " + numberFormat.format(dayWay * 100) + "km");

                lastDateStart = pointGc;
                lastDateFinish = pointGc;

                dayIncreaseAlt = 0;
                dayDecreaseAlt = 0;
                totalWay += dayWay;
                dayWay = 0;

            } else {
                final double altDiff = lastPoint.getAltitude() - point.getAltitude();
                if (altDiff < 0) {
                    dayIncreaseAlt += -1 * altDiff;
                } else {
                    dayDecreaseAlt += altDiff;
                }

                dayWay += vectorUtils.length2D(lastPoint, point);
                lastDateFinish = pointGc;
                lastPoint = point;
            }
        }


    }

    @Override
    public void onFinish() {
        System.out.println("Track finished at: " + lastDateFinish.getTime());
        System.out.println("Total trip distance : " + numberFormat.format(totalWay * 100) + "km");
    }
}
